import cgi

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db

import urllib
from google.appengine.api import urlfetch

import sys
import httplib
import re
import string

import datastore
from datetime import date

 

class MainPage(webapp.RequestHandler):

  def Write(self,t):
    self.response.out.write(t)

  def Table(self)  :
       self.Write( '<table align="center"  class="sortable" >')

  def EndTable(self)  :
       self.Write( '</table>')

  def DoHeader(self):
     self.Write( '<tr>')
     self.Write( '<th scope="col">Name   </th>')
     self.Write( '<th scope="col">Team   </th>')
     self.Write( '<th scope="col">Win</th>')
     self.Write( '<th scope="col">Loss</th>')
     self.Write( '</tr>')

  def Cell( self,data):
     self.Write( '<td>' + str(data) + '</td>');

  def CellLink( self,id , data):
    self.Cell('<a style=text-decoration:none href="http://www.ustanorcal.com/playermatches.asp?id='+ str(id) + '">' + str(data) + '</a>')

  def TeamLink( self,id , data):
    self.Cell('<a style=text-decoration:none href="http://www.ustanorcal.com/teaminfo.asp?id='+ str(id) + '">' + str(data) + '</a>')




  def enumerate(self):

    self.Write('<html>')
    self.Write('<center>')
    self.Write('<title>3.5s Players</title>')
    self.Write('<h1>3.5s Players</h1>')
    self.Write('<style type="text/css>">')
    self.Write('</style>')
    self.Write('</head>')


    self.Table()
    self.DoHeader()

    query="select __key__ from SelfRatePlayer order by win desc"


    keys  = db.GqlQuery( query )
    for k in keys:
     t = db.get(k)
     line =  t.ustaid+t.fname+t.lname+str(t.win)+ str(t.loss) +"<br>"
     self.Write( '<tr>')        
     self.CellLink(t.ustaid ,t.fname+" "+t.lname )
     self.TeamLink(t.teamid ,t.teamname )     
     self.Cell(t.win)
     self.Cell(t.loss)
     self.Write( '</tr>')        


    self.EndTable()
    self.Write('<html>')

  def get(self):
    
      self.enumerate()


application = webapp.WSGIApplication(
                                     [('/list', MainPage)],
                                     debug=True)



def main():
    run_wsgi_app(application)

if __name__ == "__main__":

#   print __file__
#   print __name__

    main()
